package com.qyhl.webtv.module_microvideo.shortvideo.play.player;

import android.util.Pair;

public class VideoUtils {
    private static final float RATIO_FIT_CENTER = 1.6f;

    public enum ScaleType {
        /**
         * 对应于ImageView FIT_CENTER
         */
        FIT_CENTER,
        /**
         * 对应于ImageView CENTER_CROP
         */
        CENTER_CROP
    }

    public static ScaleType getImageCropType(Pair<Integer, Integer> layoutSize,
                                             Pair<Integer, Integer> videoSize) {
        int layoutWidth = layoutSize.first;
        int layoutHeight = layoutSize.second;
        int videoWidth = videoSize.first;
        int videoHeight = videoSize.second;

        float layoutAspectRatio = (float) layoutHeight / (float) layoutWidth;
        float videoAspectRatio = (float) videoHeight / (float) videoWidth;

        if (layoutAspectRatio / videoAspectRatio >= RATIO_FIT_CENTER) {
            return ScaleType.FIT_CENTER;
        } else {
            return ScaleType.CENTER_CROP;
        }
    }


    static Pair<Integer, Integer> getFitSize(Pair<Integer, Integer> layoutSize, Pair<Integer, Integer> videoSize) {

        int layoutWidth = layoutSize.first;
        int layoutHeight = layoutSize.second;
        int videoWidth = videoSize.first;
        int videoHeight = videoSize.second;

        float layoutAspectRatio = (float) layoutHeight / (float) layoutWidth;
        float videoAspectRatio = (float) videoHeight / (float) videoWidth;

        if (layoutAspectRatio >= videoAspectRatio && (layoutAspectRatio / videoAspectRatio < RATIO_FIT_CENTER)) {
            int fitWidth = (int) (layoutHeight / videoAspectRatio);
            return new Pair<>(fitWidth, layoutHeight);
        } else {
            int fitHeight = (int) (layoutWidth * videoAspectRatio);
            return new Pair<>(layoutWidth, fitHeight);
        }
    }


}
